package com.callback.confirmreturndemo.controller;

import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.connection.CorrelationData;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.PostConstruct;

@Slf4j
@RestController
public class CallBack2SendMessage  implements RabbitTemplate.ConfirmCallback,RabbitTemplate.ReturnCallback{
    @Autowired
    private RabbitTemplate rabbitTemplate;
    //未验证
    @PostConstruct
    public void init(){
        rabbitTemplate.setReturnCallback(this::returnedMessage);
        rabbitTemplate.setConfirmCallback(this::confirm);
    }
    @Override
    public void confirm(CorrelationData correlationData, boolean ack, String cause) {
        if (ack){
            log.info("消息送达交换机 ："+correlationData.getId());

        }else {
            log.error("消息未送达交换机："+correlationData.getId());
            log.error("消息未送达交换机理由："+cause);
        }
    }

    @Override
    public void returnedMessage(Message message, int replyCode, String replyText, String exchange, String routingKey) {
        log.error("消息未送达oqueue message:{},replyCode:{},replyText:{},exchange:{},routingKey:{}",
                message.getBody(),
                replyCode,
                replyText,
                exchange,
                routingKey);
    }
}
